package baduga.karo;

//klasa z pierwszym ekranem gracza - listą stołów

import java.awt.Toolkit;

/**
  * @author Karolina
 */
public class TableListScreen extends javax.swing.JFrame {

  
	private static final long serialVersionUID = 1L;
	public TableListScreen() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(TableListScreen.class.getResource("/baduga/karo/resources/icon.png")));
        initComponents();
    }

    @SuppressWarnings("unchecked")
                              
    private void initComponents() {

    	//dodajemy podstawowe pola na oknie
    	
        jButtonJoinGame = new javax.swing.JButton();
        jButtonNewTable = new javax.swing.JButton();
        jButtonDownExit = new javax.swing.JButton();
        jLabelTittle = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTables = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenuFIile = new javax.swing.JMenu();
        jMenuItemExit = new javax.swing.JMenuItem();
        jMenuGame = new javax.swing.JMenu();
        jMenuItemNewTable = new javax.swing.JMenuItem();
        jMenuHelp = new javax.swing.JMenu();
        jMenuItemHelp = new javax.swing.JMenuItem();
        jMenuItemAbout = new javax.swing.JMenuItem();

        //po wciśnięciu X okienko zamyka się, ustalamy tytuł
        
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Badugi - Lista stołów");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
            //    windowOpened(evt);
            }
        });

        //dodajemy akcję to danego przycisku
        
        
        jButtonJoinGame.setText("Dołącz");
        jButtonJoinGame.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonJoinGameMouseClicked(evt);
            }
        });

        jButtonNewTable.setText("Nowy stół");
        jButtonNewTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonNewTableMouseClicked(evt);
            }
        });

        jButtonDownExit.setText("Wyjście");
        jButtonDownExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDownExitActionPerformed(evt);
            }
        });
        
        
        jLabelTittle.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabelTittle.setText("Badugi - lista stołów");

      
        //pobieramy listę stołów oraz graczy i wyświetlamy ją w tabeli
        
        /* w zmiennej numberOfTables przechowujemy ilość stołów
         * następnie tablica numberOfPlayers mówi nam ilu graczy jest na danym stole (podmieniamy ich nicki, reszta zostaje jako "-"
         * następnie tablica playersNames mówi nam o tym jakie nicki mają gracze na danym stole (nr indeksu = nr stołu))
         * w pierwszej pętli for do tablesList podstawiamy wszędzie myślniki
         * a w drugiej ładnie zczytujemy od serwera nicki
         * oczywiście numberOfPlayers można zastąpić playersNames[i].length - będzie to ilość graczy na i-tym stole
         * w tablicy tablesNames mamy nazwy stołów - i-ty indeks to nazwa i-tego stołu
         */
        
        int numberOfTables = 0;
        int[] numberOfPlayers = new int[numberOfTables];
        String[][] playersNames = new String[numberOfTables][7];
        String[] tablesNames = new String[numberOfTables];
        Object[][] tablesList = new Object[numberOfTables][7];
        
       for(int i = 0; i<numberOfTables; i++){
    	   for(int j = 0; j <7;j++ ){
    		   tablesList[i][j] = "-----------";
    	   }
       }
        
        for(int i = 0; i < numberOfTables; i++){
        	for(int j = 0; j+1< numberOfPlayers[i]; j++){
        		tablesList[i][0] = tablesNames[i];
        		tablesList[i][j+1] = playersNames[i][j];
        	}
        }
        
        String[] columnNames = {"Nazwa stołu", "Gracz 1", "Gracz 2", "Gracz 3", "Gracz 4", "Gracz 5", "Gracz 6"};
        
        jTables.setModel(new javax.swing.table.DefaultTableModel(tablesList, columnNames){
        
        	// blokujemy edycją rekordów
        boolean[] canEdit = new boolean [] {
            false, false, false, false, false, false, false
        };
    
        public boolean isCellEditable(int rowIndex, int columnIndex) {
        	return canEdit [columnIndex];
        	}
        });
            
        //rozmiar kolumny tabeli z nazwa stołu wbijamy na sztywno
        
        jTables.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTables);
        if (jTables.getColumnModel().getColumnCount() > 0) {
            jTables.getColumnModel().getColumn(0).setMinWidth(200);
            jTables.getColumnModel().getColumn(1).setResizable(false);
            jTables.getColumnModel().getColumn(2).setResizable(false);
            jTables.getColumnModel().getColumn(3).setResizable(false);
            jTables.getColumnModel().getColumn(4).setResizable(false);
            jTables.getColumnModel().getColumn(5).setResizable(false);
            jTables.getColumnModel().getColumn(6).setResizable(false);
        }
        
         //obsługa menu i jego pozycji
        
        jMenuFIile.setText("Plik");

        jMenuItemExit.setText("Zakończ");
        jMenuItemExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemExitActionPerformed(evt);
            }
        });
        jMenuFIile.add(jMenuItemExit);

        jMenuBar1.add(jMenuFIile);

        jMenuGame.setText("Gra");

        jMenuItemNewTable.setText("Nowy stół");
        jMenuItemNewTable.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemNewTableActionPerformed(evt);
            }
        });
        
        jMenuGame.add(jMenuItemNewTable);

        jMenuBar1.add(jMenuGame);

        jMenuHelp.setText("Pomoc");

        jMenuItemHelp.setText("Help");
        jMenuItemHelp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemHelpActionPerformed(evt);
            }
        });
        jMenuHelp.add(jMenuItemHelp);

        jMenuItemAbout.setText("About");
        jMenuItemAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemAboutActionPerformed(evt);
            }
        });
        jMenuHelp.add(jMenuItemAbout);

        jMenuBar1.add(jMenuHelp);

        setJMenuBar(jMenuBar1);

        //podstawowe parametry okna
        
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabelTittle, javax.swing.GroupLayout.PREFERRED_SIZE, 448, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(218, 218, 218)
                                .addComponent(jButtonJoinGame, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(27, 27, 27)
                                .addComponent(jButtonNewTable)
                                .addGap(26, 26, 26)
                                .addComponent(jButtonDownExit)))
                        .addGap(0, 399, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelTittle, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonJoinGame)
                    .addComponent(jButtonNewTable)
                    .addComponent(jButtonDownExit))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        pack();
    }
    
    	//obsluga przycisku wyjscie
    private void jButtonDownExitActionPerformed(java.awt.event.ActionEvent evt) {                                                
        final ExitScreen question = new ExitScreen();
            question.setVisible(true);
            question.setLocationRelativeTo(null);
    }                                               

   

    //obsluga przycisku nowy stol
    private void jButtonNewTableMouseClicked(java.awt.event.MouseEvent evt) {                                             
        
         final CreateNewTable NewTable = new CreateNewTable();
            NewTable.setVisible(true);
            NewTable.setLocationRelativeTo(null);
            this.setVisible(false);            
    }     
    
 //obsluga przycisku dolacz
    private void jButtonJoinGameMouseClicked(java.awt.event.MouseEvent evt) {                                             
        //otwiera dany stolik
            
    }                                            
// obsluga przycisku wyjdz w menu
    private void jMenuItemExitActionPerformed(java.awt.event.ActionEvent evt) {                                              
        final ExitScreen question = new ExitScreen();
            question.setVisible(true);
            question.setLocationRelativeTo(null);
    }                                             
//zasady gry
    private void jMenuItemHelpActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
    }                                             
//o autorach
    private void jMenuItemAboutActionPerformed(java.awt.event.ActionEvent evt) {                                               
        final AboutAuthorsScreen about = new AboutAuthorsScreen();
            about.setVisible(true);
            about.setLocationRelativeTo(null);
    }                                              
//obsluga pozycji nowy stol w menu
    private void jMenuItemNewTableActionPerformed(java.awt.event.ActionEvent evt) {                                               
    	final CreateNewTable NewTable = new CreateNewTable();
        NewTable.setVisible(true);
        NewTable.setLocationRelativeTo(null);
        this.setVisible(false);      
    }                                              

    
    public static void main(String args[]) {
    	//zmieniamy styl okienek, jeżeli ten będzie niedostępny ładowany jest domyślny
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TableListScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TableListScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TableListScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TableListScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
      
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
            		TableListScreen lists = new TableListScreen();
            		lists.setVisible(true);
            		lists.setLocationRelativeTo(null); 
            		lists.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
                lists.addWindowListener(new java.awt.event.WindowAdapter() {
            });
     
         
                lists.addWindowListener(
                        new java.awt.event.WindowAdapter() {
                            @Override
                            public void windowClosing(java.awt.event.WindowEvent e) {
                                final ExitScreen question = new ExitScreen();
                                question.setVisible(true);
                                question.setLocationRelativeTo(null);
                              
                                
                            }
                        }
                );
        
            }
            
        });}

                     
    private javax.swing.JButton jButtonDownExit;
    private javax.swing.JButton jButtonJoinGame;
    private javax.swing.JButton jButtonNewTable;
    private javax.swing.JLabel jLabelTittle;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenu jMenuFIile;
    private javax.swing.JMenu jMenuGame;
    private javax.swing.JMenu jMenuHelp;
    private javax.swing.JMenuItem jMenuItemAbout;
    private javax.swing.JMenuItem jMenuItemExit;
    private javax.swing.JMenuItem jMenuItemHelp;
    private javax.swing.JMenuItem jMenuItemNewTable;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTables;
               
}
